#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int monotoneIncreasingDigits(int n) {
        string sn = to_string(n);
        int pos = 0;
        for (; pos < sn.size() - 1; pos++)
            if (sn[pos] > sn[pos + 1]) break;
        int ret = 0;
        if (pos == sn.size() - 1) return n;
        while (pos > 0) if (sn[pos - 1] == sn[pos]) pos--; else break;
        for (int i = 0; i < pos; i++) ret = ret * 10 + sn[i] - '0';
        ret = ret * 10 + sn[pos] - '0' - 1;
        for (int i = pos + 1; i < sn.size(); i++) ret = ret * 10 + 9;
        return ret;
    }
};